package com.kgc.controller;


import com.alibaba.fastjson.JSON;
import com.kgc.service.ICategoryService;
import entity.Category;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.product.categoryservice.CategoryAPI;

import java.util.List;


/**
 * @description:  商品分类信息的相关方法  ,提供者
 *
 * @author: Cheng
 *
 * @create: 2022-04-13 13:58
 **/
@Controller
public class CategoryProviderController implements CategoryAPI {


    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private ICategoryService iCategoryService;


    /**
     * 分页 ,查询分类信息
     * @param pageNo    当前页
     * @param pageSize  页面大小
     * @param cname
     * @param type
     * @param parentId
     * @return ResponseMessage
     */
    @Override
    @RequestMapping("selectAll")
    @ResponseBody
    public ResponseMessage selectALl(@RequestParam(value = "pageNo",defaultValue = "1")int pageNo,
                                     @RequestParam(value = "pageSize",defaultValue = "5")int pageSize,
                                     @RequestParam(value = "cname",defaultValue = "")String cname,
                                     @RequestParam(value = "type",defaultValue = "0") int type,
                                     @RequestParam(value = "parentId",defaultValue = "0")int parentId) {
        logger.info("CategoryProviderController selectALl is start ...params:pageNo-->"+pageNo+",pageSize-->"+pageSize+",canme-->"+cname+",type:-->"+type+",parentId-->"+parentId);
        ResponseMessage responseMessage = iCategoryService.selectALL(pageNo, pageSize,cname,type,parentId);
        logger.debug("CategoryProviderController selectALl result:" + responseMessage);
        return responseMessage;
    }

    /**
     * 添加一条商品分类
     * @param category
     * @return  ResponseMessage
     */
    @Override
    @RequestMapping("insertCategoryInfo")
    @ResponseBody
    public ResponseMessage insertCategoryInfo(@RequestBody Category category) {
        logger.info("CategoryProviderController insertCategoryInfo is start.....param:category-->" + category);
        ResponseMessage responseMessage = iCategoryService.insertCategoryInfo(category);
        logger.debug("CategoryProviderController insertCategoryInfo result:responseMessage-->"+responseMessage);
        return responseMessage;
    }


    /**
     * 更新一条商品分类信息
     * @param category
     * @return
     */
    @Override
    @RequestMapping("updateCategoryInfo")
    @ResponseBody
    public ResponseMessage updateCategoryInfo(@RequestBody Category category) {
        logger.info("CategoryProviderController updateCategoryInfo is start.....param:category-->" + category);
        ResponseMessage responseMessage = iCategoryService.updateCategoryInfo(category);
        logger.debug("CategoryProviderController updateCategoryInfo result:responseMessage-->"+responseMessage);
        return responseMessage;
    }



    /**
     *删除单条商品分类
     * @param id
     * @return  ResponseMessage
     */
    @Override
    @RequestMapping("freezeCategoryInfo")
    @ResponseBody
    public ResponseMessage freezeCategoryInfo(@RequestParam("id") String id ) {
        logger.info("CategoryProviderController freezeCategoryInfo is start.....param:category-->" + id);
        ResponseMessage responseMessage = iCategoryService.freezeCategoryInfo(id);
        logger.debug("CategoryProviderController freezeCategoryInfo result:responseMessage-->"+responseMessage);
        return responseMessage;
    }

    /**
     * 查询所有商品分类的type
     * @return String
     */
    @Override
    @RequestMapping("selectByCategoryType")
    @ResponseBody
    public String selectByCategoryType() {
        logger.info("CategoryProviderController selectByCategoryType is start");
        List<Category> typeList = iCategoryService.selectByCategoryType();
        logger.debug("CategoryProviderController selectByCategoryType result:list-->"+typeList);
        return JSON.toJSONString(typeList);
    }


    /**
     * 通过type查找所有的商品分类信息
     * @param type
     * @return ResponseMessage
     */
    @Override
    @RequestMapping("selectInsetPageByType")
    @ResponseBody
    public ResponseMessage selectInsetPageByType(@RequestParam(value = "type")  int type) {
        logger.info("CategoryProviderController selectInsetPageByType is start .... param:type"+type);
        if (type < 0) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("Param: type is less than zero ");
            return responseMessage;
        }
        ResponseMessage responseMessage =iCategoryService.selectInsetPageByType(type);
        logger.debug("CategoryProviderController selectInsetPageByType result:responseMessage-->"+responseMessage);
        return responseMessage;
    }


    /**
     * 通过type查找所有的商品分类信息,排除id的
     * @param type
     * @param id
     * @return ResponseMessage
     */
    @Override
    @RequestMapping("selectUpdatePageByType")
    @ResponseBody
    public ResponseMessage selectUpdatePageByType(@RequestParam("type")  int type ,@RequestParam("id")String id){
        logger.info("CategoryProviderController selectUpdatePageByType is start .... params:type-->"+type+",id:-->"+id);
        if (type < 0) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("Param: type is less than zero ");
            return responseMessage;
        }
        ResponseMessage responseMessage =iCategoryService.selectUpdatePageByType(type,id);
        logger.debug("CategoryProviderController selectUpdatePageByType result:responseMessage-->"+responseMessage);
        return responseMessage;
    }


    /**
     * 通过id查询单条数据
     * @param id
     * @return ResponseMessage
     */
    @Override
    @RequestMapping("selectCategoryById")
    @ResponseBody
    public ResponseMessage selectCategoryById(@RequestParam("id") String id) {
        logger.info("CategoryProviderController selectCategoryById is start .... param:id"+id);
        if (id==null||"".equals(id)) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("Param: type is less than zero ");
            return responseMessage;
        }
        ResponseMessage responseMessage =iCategoryService.selectCategoryById(id);
        logger.debug("CategoryProviderController selectInsetPageByType result:responseMessage-->"+responseMessage);
        return responseMessage;
    }

    /**
     * 查询cname查询所有分类信息
     * @param cname
     * @return
     */
    @Override
    @RequestMapping("selectInsetPageByCname")
    @ResponseBody
    public ResponseMessage selectInsetPageByCname(@RequestParam("cname") String cname){
        logger.info("CategoryProviderController selectCategoryById is start .... param:cname"+cname);
        if (cname==null||"".equals(cname)) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setFlag(false);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("Param: type is null ");
            return responseMessage;
        }
        ResponseMessage responseMessage =iCategoryService.selectInsetPageByCname(cname);
        logger.debug("CategoryProviderController selectInsetPageByType result:responseMessage-->"+responseMessage);
        return responseMessage;
    }


}
